package com.example.ivos.user.controller;

import com.example.ivos.base.response.JsonResult;
import com.example.ivos.user.pojo.dto.UserLoginParam;
import com.example.ivos.user.pojo.dto.UserQuery;
import com.example.ivos.user.pojo.dto.UserSaveParam;
import com.example.ivos.user.pojo.vo.UserVO;
import com.example.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    UserService userService;//注意:此处是多态的用法,写的是接口,实际上使用的是接口的子实现类!

    @PostMapping("/login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam){
        log.debug("用户登录:userLoginParam={}",userLoginParam);
        UserVO userVO = userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }
    @PostMapping("/save")
    //前端传过来的数据已经由qs(QueryString)转为了查询字符串格式
    //所以不需要使用@RequstBody注解进行映射了
    //public JsonResult saveUser(@RequestBody UserSaveParam userSaveParam){
    public JsonResult saveUser(UserSaveParam userSaveParam){
        log.debug("保存用户,userSaveParam={}",userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }
    @GetMapping("/select")
    public JsonResult selectUser(UserQuery userQuery){
        log.debug("查询用户参数:{}",userQuery);
        List<UserVO> list = userService.selectUsers(userQuery);
        return JsonResult.ok(list);
    }
    @PostMapping("/update/password/{userId}")
    public JsonResult updatePassword(@PathVariable("userId") Long userId,String password){
        log.debug("修改密码,userId={},password={}",userId,password);
        userService.updatePassword(userId);
        return JsonResult.ok();
    }
    @PostMapping("/update/status/{userId}/{status}")
    public JsonResult updateStatus(@PathVariable("userId") Long userId,@PathVariable("status") String status){
        log.debug("修改状态,userId={},status={}",userId,status);
        userService.updateStatus(userId,status);
        return JsonResult.ok();
    }
    @PostMapping("/delete/{userId}")
    public JsonResult delete(@PathVariable  Long userId){
        log.debug("删除用户,userId={}",userId);
        userService.delete(userId);
        return JsonResult.ok();
    }
    @GetMapping("/select/audit/{parentId}")
    public JsonResult selectAudit(@PathVariable Long parentId){
        log.debug("查询待审核用户parentId={}",parentId);
        ArrayList<UserVO> list = userService.selectAudit(parentId);
        log.debug("查询待审核用户结果:{}",list);
        return JsonResult.ok(list);
    }
}